package com.guli.gulimall.product.util;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @ClassName GetCatalogIds
 * @Author Hao_YunKai
 * @Date 2021/12/17 20:16
 */
@Component
public class CategoryUtil {

    @Autowired
    private CategoryDao categoryDao;

    public Long[] getCatalogIds(Long catalogId) {
        ArrayList<Long> list = new ArrayList<>();
        getLogPath(list,catalogId);
        //反转 123 321
        Collections.reverse(list);
        //转Long数组
        Long[] ids = list.toArray(new Long[list.size()]);
        return ids;
    }

    //查询分类表
    public void getLogPath(ArrayList<Long> list, Long catalogId) {
        list.add(catalogId);
        CategoryEntity categoryEntity = categoryDao.selectById(catalogId);
        if (!categoryEntity.getParentCid().equals(0L)) {
            getLogPath(list,categoryEntity.getParentCid());
        }
    }
}
